/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.dialogs;

import java.awt.Component;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.jfonia.language.LanguageResource;
import org.jfonia.language.MessageConstants;

/**
 *
 * @author Rik Bauwens
 */
public class OpenFileDialog extends JFileChooser
{
        private static OpenFileDialog instance;
        private Component parent;

        private OpenFileDialog()
        {
                super();
                setAcceptAllFileFilterUsed(false);
        }

        public static synchronized OpenFileDialog getInstance()
        {
                if(instance == null)
                        instance = new OpenFileDialog();
                return instance;
        }

        public void setParent(Component parent)
        {
                this.parent = parent;
        }

        public String open()
        {
                String fileName = "";
                int state;
                Boolean done = false;
                while(!done)
                {
                        state = this.showOpenDialog(parent);
                        if(state == JFileChooser.APPROVE_OPTION)
                        {
                                File f = getSelectedFile();
                                if(f.exists())
                                {
                                        fileName = f.getAbsolutePath();
                                        done = true;
                                }
                                else
                                {
                                        JOptionPane.showMessageDialog(parent, 
                                                LanguageResource.getInstance().getMessage(
                                                MessageConstants.FILE_DOES_NOT_EXIST));
                                }
                        }
                        else
                        {
                                done = true;
                        }
                }
                return fileName;
        }
}
